﻿Imports HMSLibrary
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data
Imports HMSDataLayer

Public Class BanksControl
    Implements IHMSData

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        DataValidation = False
        Try
            If Trim(NameTextBox.Text) = "" Then
                CommonMessages.ShowValidationMsg("Please enter name!")
                Exit Function
            End If

            If ActionMode = FormAction.ADD Then
                Dim strSql As String
                strSql = "SELECT  [BankID] FROM dbo.BankMaster WHERE  ([BankName] = N'" & NameTextBox.Text.Trim & "') AND ([BranchName] = N'" & BranchNameTextBox.Text.Trim & "') "
                Dim objTemp As Object = Nothing
                objTemp = AppCommon.db.ExecuteScalar(CommandType.Text, strSql)
                If objTemp IsNot Nothing Then
                    If IsDBNull(objTemp) = False Then
                        CommonMessages.ShowValidationMsg("Bank already exist!")
                        Exit Function
                    End If
                End If
            End If
            DataValidation = True
        Catch ex As Exception
            AppCommon.LogError(ex)
        End Try
    End Function

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData
        ActionMode = FormAction.EDIT
    End Sub

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        If DataValidation() = False Then Return False
        Dim dataProvider As New MasterDataLayer()
        Dim bank As New Banks
        bank.Name = NameTextBox.Text
        bank.Address = AddressTextBox.Text
        bank.BranchName = BranchNameTextBox.Text
        bank.TelNo = TelNoTextBox.Text
        If dataProvider.SaveBanks(bank, ActionMode) = True Then
            Return True
        End If
    End Function

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode

    Private Sub ClientEntryUserControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        ActionMode = FormAction.ADD
    End Sub

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        NameTextBox.Text = String.Empty
        AddressTextBox.Text = String.Empty
        BranchNameTextBox.Text = String.Empty
        TelNoTextBox.Text = String.Empty
        ActionMode = FormAction.ADD
    End Sub

    Public Sub FindData() Implements IHMSData.FindData
        MainGrid.Visibility = Visibility.Collapsed
        FindStackPanel.Visibility = Visibility.Visible
        Dim DataObject As MasterDataLayer
        Dim DataCollection As Object
        DataObject = New MasterDataLayer()
        DataCollection = DataObject.GetBanksList
        SearchDataGrid.ItemsSource = DataCollection
    End Sub

    Public Sub BackView() Implements IHMSData.BackView
        MainGrid.Visibility = Visibility.Visible
        FindStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub
End Class
